const innerAudioContext = my.createInnerAudioContext();
Page({

  data: {
    shop_id: '',
    shop_title: '明志科技',
    topImage: 'https://img.alicdn.com/imgextra/i1/376771766/O1CN01ZJFwoQ1Ous2SQamAS_!!376771766.png',
    mainText: '邀请好友帮拆100%赢精美好礼',
    bgMusic: 'https://gshow.ewszjk.m.jaeapp.com/Public/music/good_luck.mp3',
    boxImage: 'https://img.alicdn.com/imgextra/i4/376771766/O1CN01lkS8Dk1Ous2NVcPMP_!!376771766.png ',
    maxLimit: '1000',
    personNum: '11',
    pageView: '1235',
    percent: '80',
    status: 0,
    start:'2020-01-01',
    end:'2020-01-31',
    timeStamp: 20000,
    pool: {
      name1: '优惠劵100元',
      image1: 'http://cdn.mzyun.tech/coupon.jpeg',
      name2: '优惠劵50元',
      image2: 'http://cdn.mzyun.tech/coupon.jpeg',
      name3: '优惠劵20元',
      image3: 'http://cdn.mzyun.tech/coupon.jpeg',
    },
    instruction: ['1.本次活动为促销活动,严禁恶意作弊行为，一经发现，取消中奖资格', '2.本次活动所赠送的实物奖品均为本店铺所售商品，欢迎各位客户浏览并收藏我们的店铺'],
    phone: '01086466630',
    music: false

  },
  onLoad(query) {
    if (query.box_id) {
      let box_id=query.box_id, main_id=query.main_id;
      this.addLog(box_id,main_id);
      my.alert({
        title: '您已为好友成功助力，现在为自己领一个礼盒吧！'
      })
    }
    //console.info(`Page onLoad with query: ${JSON.stringify(query)}`);
    this.loadData();
    this.addPageView()



  },
  onReady() {
    // 页面加载完成
  },
  onShow() {
    innerAudioContext.autoplay = true
    innerAudioContext.src = this.data.bgMusic
    innerAudioContext.loop=true
    innerAudioContext.onPlay(() => {
      console.log('开始播放')
      this.setData({
        music: true
      })
    })
    innerAudioContext.onPause(() => {
      console.log('暂停播放')
      this.setData({
        music: false
      })
    })
    innerAudioContext.onError((res) => {
      console.log(res.errMsg)
    })
  },
  onHide() {
    innerAudioContext.destroy()
  },
  onUnload() {

  },
  onTitleClick() {
    // 标题被点击
  },
  onPullDownRefresh() {
    // 页面被下拉
  },
  onReachBottom() {
    // 页面被拉到底部
  },
  onShareAppMessage() {
    // 返回自定义分享信息
    return {
      title: '拆礼盒，百分百有礼',
      desc: this.data.shop_title + '店铺促销，邀请您赢好礼',
      path: `/pages/index/index`,
    };
  },
  goShop() {
    my.tb.navigateToTaobaoPage({
      appCode: 'shop',
      appParams: {
        shopId: this.data.shop_id
      },
      success: (res) => {
      },
      fail: (res) => {
      }
    })
  },

  goMy() {
    my.navigateTo(
      { url: `../my/my?boxImage=${this.data.boxImage}` }
    );
  },
  goRank() {
    my.navigateTo(
      { url: '../rank/rank' }
    );
  },
  musicSwitch() {
    if (this.data.music == true) {
      innerAudioContext.pause()
    } else {
      innerAudioContext.play()
    }

  },
  makePhoneCall() {
    my.makePhoneCall({
      number: this.data.phone
    })
  },
  async addLog(box_id,main_id) {
    const { cloud } = getApp();
    const res = await cloud.application.httpRequest({
      path: '/index.php/index/tb/addlog',
      method: 'POST',
      params: {
        shop_id:getApp().globalData.shop_id,
        box_id: box_id,
        main_id:main_id
      }
    });
    this.goMy();
  },
  async getBox() {
    const { cloud } = getApp();
    const res = await cloud.application.httpRequest({
      path: '/index.php/index/tb/getbox',
      method: 'GET',
      params:{
        shop_id:getApp().globalData.shop_id
      }
    });
    this.goMy();
  },
  share() {
    my.showSharePanel();
  },
  async loadData() {
    const { cloud } = getApp();
    const res = await cloud.application.httpRequest({
      path: '/index.php/index/tb/index',
      method: 'GET',
      params:{
        shop_id:getApp().globalData.shop_id
      }
    });
    console.log(res)
    this.setData({
      shop_id: res.shop_id,
      shop_title: res.shop_title,
      topImage: res.topImage,
      mainText: res.mainText,
      bgMusic: res.bgMusic,
      boxImage: res.boxImage,
      status: res.status,
      timeStamp: res.timeStamp,
      maxLimit: res.max_limit,
      personNum: res.person_num,
      pageView: res.page_view,
      percent: res.percent,
      personLimit:res.personLimit,
      dayLimit:res.dayLimit,
      pool: res.pool,
      instruction: res.instruction,
      phone: res.phone
    })
  },
  async addPageView() {
    const { cloud } = getApp();
    const res = await cloud.application.httpRequest({
      path: '/index.php/index/tb/addpageview',
      method: 'GET',
      params:{
        shop_id:getApp().globalData.shop_id
      }
    })
  }

});
